<template>
  <div class="bindPhone">
    <van-form @submit="onSubmit">
      <div class="bindingBox bg_w border_r8">
        <div class="f_16 tc_1 fw_700 margin_b20">绑定手机号</div>
        <div>
          <van-field class="itemInput f_14" v-model="phoneNum" name="phone" placeholder="请输入您的手机号" :rules="[{required: true, validator: validatorPhone, trigger: 'onBlur'}]" />
        </div>
      </div>
      <div style="margin-top: 2rem">
        <van-button class="submitBtn f_16" color="#31A2A3" :disabled="btnDisable" :loading="btnDisable" round block type="info" native-type="submit"> 提交资料 </van-button>
      </div>
    </van-form>
    <!-- <div class="bindingBox bg_w border_r8">
      <div class="f_16 tc_1 fw_700 margin_b20">绑定手机号</div>
      <div>
        <van-field class="itemInput f_14" v-model="phoneNum" name="phone" placeholder="请输入您的手机号" :rules="[{required: true, validator: validatorPhone, message: '请输入正确手机号', trigger: 'onBlur'}]" />
      </div>
    </div>
    <div class="btnBox">
      <div class="btn bg_g tc_w f_16" @click="bindingPhone">确定</div>
    </div> -->
    <div class="footer">
      <div class="footerLogo">
        <van-image width="6.8rem" height="1.6rem" fit="cover" :src="require('@/assets/logo_qianyu.png')" />
      </div>
    </div>
  </div>
</template>

<script>
import {mapGetters} from 'vuex'
import loginApi from '@/api/login'
import {getOpenId} from '@/utils/auth'
export default {
  data() {
    return {
      phoneNum: '',
      btnDisable: false
    }
  },
  computed: {
    ...mapGetters(['token', 'userInfo', 'status']),
    userInfo() {
      return this.userInfo
    }
  },
  methods: {
    validatorPhone(val) {
      return /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|19[0-9]|14[57])[0-9]{8}$/.test(val)
    },
    onSubmit(data) {
      let _this = this
      _this.btnDisable = true
      let openId = getOpenId()
      loginApi.bindingMobile({mobile: this.phoneNum, open_id: openId}).then((res) => {
        if (res.code === 0) {
          this.$dialog
            .alert({
              title: '提示',
              message: res.msg,
              messageAlign: 'center',
              confirmButtonColor: '#31A2A3'
            })
            .then(() => {})
        } else if (this.$tools.isNullOrEmpty(res.data)) {
          // data为null则表示用户不存在, 需要跳转到信息完善页面操作
          this.$dialog
            .alert({
              title: '提示',
              message: '您还不是我们的用户,\n请先完善信息!',
              messageAlign: 'center',
              confirmButtonColor: '#31A2A3'
            })
            .then(() => {
              this.$router.push({
                path: '/userInforEntry'
              })
            })
        } else {
          // data里面有信息
          this.$store.dispatch('setLoginData', res)
          this.$router.push({
            path: '/booking'
          })
        }
        _this.btnDisable = false
      })
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="less" scoped>
.bindPhone {
  padding: 0.8rem 1.4rem;
  box-sizing: border-box;
  .bindingBox {
    padding: 2rem;
    box-sizing: border-box;
    margin-bottom: 5rem;
    .itemInput {
      background-color: #f5f5f5;
      border-radius: 0.4rem;
      min-height: 4.6rem;
    }
  }
}
.submitBtn {
  height: 4rem;
}
// .btnBox {
//   .btn {
//     width: 34.3rem;
//     height: 4rem;
//     line-height: 4rem;
//     text-align: center;
//     border-radius: 4rem;
//     margin: 1rem auto;
//   }
// }
.footer {
  width: 100%;
  height: 6rem;
  position: fixed;
  bottom: 0;
  left: 0;
  .footerLogo {
    width: 34.3rem;
    height: 4rem;
    line-height: 4rem;
    text-align: center;
    border-radius: 4rem;
    margin: 1rem auto;
  }
}
</style>
